﻿using DataCollection.WinForm.ViewModel;
using DataCollection.Infrastructure;

namespace DataCollection.WinForm.Forms
{
    public partial class AddFormPointForm : DCBaseForm
    {
        #region 属性

        private FormPointViewModel viewModel = null!;

        /// <summary>
        /// 需要绑定的控件
        /// </summary>
        private Dictionary<string, Control> ControlDict = new Dictionary<string, Control>();

        public DevicePointItem? SelectDevicePointItem;
        #endregion

        #region 自定义方法

        public void LoadDevicePointInfo()
        {
            if (SelectDevicePointItem is not null)
            {
                DeviceNumberInput.Text = SelectDevicePointItem.Number;
            }
            DeviceNumberInput.Enabled = true;
        }

        #endregion

        #region 系统事件
        public AddFormPointForm()
        {
            InitializeComponent();
            Header.Text = "添加界面点位";
            ControlDict.Add("number", NumberInput);
            ControlDict.Add("name", NameInput);
            ControlDict.Add("devicePointNumber", DeviceNumberInput);
            ControlDict.Add("formControlName", ControlNameInput);
            ControlDict.Add("conver", ConverSelect);
        }

        private void AddFormPointForm_Load(object sender, EventArgs e)
        {
            viewModel = new FormPointViewModel(DateTimeOffset.Now.ToUnixTimeSeconds());
            foreach (var item in ControlDict)
            {
                Bindings(item.Value, "Text", item.Key, viewModel);
            }
        }
        #endregion

        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            viewModel.Apply();
            Response response = viewModel.Add();
            if (response.Code == Response.SuccessCode)
            {
                if (this.Owner != null)
                {
                    ManageFormPointForm frm = (ManageFormPointForm)this.Owner;
                    frm.LoadList();
                    frm.Success(response.Msg);
                }
                this.Close();
            }
            else
            {
                Error(response.Msg);
            }
        }

        private void DeviceNumberInput_Click(object sender, EventArgs e)
        {
            DeviceNumberInput.Enabled = false;
            ManageDeviceForm frm = new ManageDeviceForm();
            frm.Owner = this;
            frm.Type = 1;
            frm.ShowDialog();
        }
    }
}
